package tech.aistar.controller.demand;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.entity.*;
import tech.aistar.service.center.IUserInfoService;
import tech.aistar.service.demand.IDemandService;
import tech.aistar.service.demand.IPictureService;
import tech.aistar.service.order.IDemandOrderService;
import tech.aistar.service.services.IServicesService;
import tech.aistar.util.MakeOrderNum;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * Created by 豆梦寒 on 2019/10/4.
 */
@SuppressWarnings("all")
@Controller
@RequestMapping("/demand")
public class demandDetailController {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IDemandService demandService;

    @Autowired
    private IDemandOrderService demandOrderService;

    @Autowired
    private IPictureService iPictureService;

    @Autowired
    private IServicesService servicesService;

    @GetMapping("/demandDetailView")
    public String demandDetail(Model model,Integer id){



        return "demand/demandDetail";
    }
    @ResponseBody
    @RequestMapping("/demandDetail")
    public Result demandDetail(Model model, Integer id, HttpSession session){

        Demand demand = demandService.findByd(Integer.valueOf(id));

        Demand demand1 = demandService.findByd(Integer.valueOf(id));

        UserInfo userInfo = userInfoService.ById(demand.getUserinfoId());
        Picture picture = iPictureService.findPathById(userInfo.getId());

        session.setAttribute("demand1",demand1);

        session.setAttribute("demand",demand);
        session.setAttribute("userInfo",userInfo);
        session.setAttribute("picture",picture);



        return new Result(CodeStatus.OK,true,"success",demand);
    }

    @ResponseBody
    @RequestMapping("/demandorderViewPay")
    public Result demandorder(@RequestBody DemandOrder demandOrder,HttpSession session){

          //生成订单
          DemandOrder demandOrder1 = new DemandOrder();
          String demandOrderNum = MakeOrderNum.makeOrderNum();
          demandOrder.setOrderNumber(demandOrderNum);
          demandOrder.setDemandId(demandOrder.getDemandId());
          demandOrder.setUserinfoId(demandOrder.getUserinfoId());
          demandOrder.setCommitTime(demandOrder.getCommitTime());
          demandOrder.setStatus(1);
          demandOrderService.insertDemandOrder(demandOrder);






//        session.setAttribute("serviceOrderNum",serviceOrderNum);
//        session.setAttribute("servicePrice",booking.getPrice());
//        session.setAttribute("",booking.getServicesId());

//        newsService.newsAdd(news);
//
//        System.out.println("booking = " + booking);
//        bookingService.add(booking);
//
//        return new Result(CodeStatus.OK, true, "成功!", booking);
        return new Result(CodeStatus.OK,true,"成功",demandOrder);

    }


}
